#!/bin/bash
VERSION='2.0'

GNAP_LIBDIR=${GNAP_LIBDIR:-'/usr/lib/gnap'}
source ${GNAP_LIBDIR}/gnap_shared.sh
echo "GNAP remastering tool ${NAME} ${VERSION}"

usage() {
	echo
	echo 'Usage:'
	echo "  ${NAME} [ options ]"
	echo
	echo 'Options:'
	echo '  -e extension    Extension to include in core file'
	echo '  -k kerneltbz2   Kernel to use in core file'
	echo '  -m moduletbz2   Modules to use in core file'
	echo '  -o output       Output core name'
	echo '  -g gnap_core    Original GNAP core file'
	echo '  -b basefs       basefs.tar.bz2 file to use as base'
	echo '  -d extdir       Directory where to find extensions'
	echo '  -f              Force all answers to yes (dangerous!)'
	echo
	echo "Please use man ${NAME} for more details."
}

if [[ "$#" -eq 0 || "${1}" == '-h' ]]; then
	usage
	exit 0
fi

gbegin 'Checking parameters'

# Read options
GNAP_OUTPUT=${GNAP_OUTPUT:-'mygnap-core.tar'}

while getopts ':he:k:m:o:g:b:d:f' option; do
	case ${option} in
		h ) usage
			exit 0;;
		e ) GNAP_EXTENSIONS="${GNAP_EXTENSIONS} ${OPTARG}";;
		k ) GNAP_KERNEXT="${OPTARG}";;
		m ) GNAP_MODEXT="${OPTARG}";;
		o ) GNAP_OUTPUT="${OPTARG}";;
		g ) GNAP_CORE="${OPTARG}";;
		b ) GNAP_BASEFS="${OPTARG}";;
		d ) GNAP_EXTDIR="${OPTARG}";;
		f ) GNAP_FORCEYES=1;;
		* ) gtest 1 'Specified options are incomplete or unknown!';;
	esac
done

# Root is needed
test "${EUID}" -eq 0
gtest continued $? "You need to be root to run ${NAME}"

# basefs file is required
test -f "${GNAP_BASEFS}"
gtest continued $? 'Please specify a valid GNAP basefs file (-b option)'

# core file is required
test -f "${GNAP_CORE}"
gtest continued $? 'Please specify a valid GNAP core file (-g option)'

# At least one operation is required
test -n "${GNAP_EXTENSIONS}" -o -n "${GNAP_KERNEXT}" -o -n "${GNAP_MODEXT}"
gtest $? 'Please specify at least one operation (-e, -k, -m) to perform'

# Confirm tarball overwrite
if [[ -e "${GNAP_OUTPUT}" ]]; then
	gconfirm "${GNAP_OUTPUT} already exists, overwrite"
fi

# Preparing new FS
gbegin "Unpacking ${GNAP_BASEFS} basefs"
TEMPDIR=$(mktemp -d -t gnap_remaster.XXXXXX)
gtest continued $? 'Failed to create temporary directory'
TEMPOVERDIR="${TEMPDIR}/basefs"
gmkdir "${TEMPOVERDIR}"

tar jx -C "${TEMPOVERDIR}" -f "${GNAP_BASEFS}"
gtest $? 'Failed to unpack basefs'

for overlay in ${GNAP_EXTENSIONS} ; do
	gbegin "Adding ${overlay} extension"
	test -f "${GNAP_EXTDIR}/gnapext_${overlay}.tbz2"
	gtest continued $? "${GNAP_EXTDIR}/gnapext_${overlay}.tbz2 does not exist"
	tar jxf "${GNAP_EXTDIR}/gnapext_${overlay}.tbz2" -C "${TEMPOVERDIR}"
	gtest $? "${overlay} is not a valid extension (tbz2 format)"
done

# Preparing new core
gbegin 'Extracting core tarball'
TEMPCOREDIR="${TEMPDIR}/core"
gmkdir "${TEMPCOREDIR}"

tar x -C "${TEMPCOREDIR}" -f "${GNAP_CORE}" --exclude image.squashfs
gtest $? 'Failed to extract core'

if [[ -f "${GNAP_KERNEXT}" ]]; then
	gbegin "Replacing kernel and initrd using ${GNAP_KERNEXT}"
	TEMPKERNDIR="${TEMPDIR}/kernel"
	gmkdir "${TEMPKERNDIR}"

	tar jx -C "${TEMPKERNDIR}" -f "${GNAP_KERNEXT}"
	gtest continued $? 'Failed to extract kernpackage'
	cp ${TEMPKERNDIR}/kernel* "${TEMPCOREDIR}/isolinux/gentoo" && \
		cp ${TEMPKERNDIR}/initr* "${TEMPCOREDIR}/isolinux/gentoo.igz"
	gtest $? 'Failed to copy kernel and initrd'

elif [[ -n "${GNAP_KERNEXT}" ]]; then
	gwarn "${GNAP_KERNEXT} does not exist, ignoring..."
fi

if [[ -f "${GNAP_MODEXT}" ]]; then
	gbegin "Replacing modules using ${GNAP_MODEXT}"
	rm -rf "${TEMPOVERDIR}/lib/modules" &&
		tar jx -C "${TEMPOVERDIR}" -f "${GNAP_MODEXT}"
	gtest $? 'Failed to replace modules'
elif [[ -n "${GNAP_MODEXT}" ]]; then
	gwarn "${GNAP_MODEXT} does not exist, ignoring..."
fi

gbegin 'Creating new squashfs filesystem'
mksquashfs "${TEMPOVERDIR}" "${TEMPCOREDIR}/image.squashfs" -noappend \
	> /dev/null 2>/dev/null
gtest $? 'Failed to create squashfs'

gbegin "Creating ${GNAP_OUTPUT} core file"
tar cf "${GNAP_OUTPUT}" -C "${TEMPCOREDIR}" .
gtest $? 'Failed to create new core tarball'

# Successful finish
cleanup
echo 'Remaster successful!'
exit 0

